package com.szpx.controller;

import com.szpx.entity.TBUser;
import com.szpx.entity.TbComment;
import com.szpx.service.TbCommentService;
import com.szpx.vo.TbCommentVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * (TbComment)表控制层
 *
 * @since 2024-04-07 23:09:27
 */
@RestController
@RequestMapping("tbComment")
public class TbCommentController {
    /**
     * 服务对象
     */
    @Resource
    private TbCommentService tbCommentService;

    /**
     * 分页查询
     *
     * @param tbComment 筛选条件
     * @return 查询结果
     */
    @GetMapping("/queryAll")
    public ResponseEntity<List<TbCommentVo>> queryAll(TbComment tbComment) {
        return ResponseEntity.ok(this.tbCommentService.queryAll(tbComment));
    }


    /**
     * 新增数据
     *
     * @param tbComment 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public ResponseEntity<Integer> add(@RequestBody TbComment tbComment, HttpSession session) {
        if(session.getAttribute("user") == null){
            return ResponseEntity.ok(-1);
        }
        TBUser user = (TBUser) session.getAttribute("user");
        tbComment.setCreateTime(new Date());
        tbComment.setUserId(user.getUid());
        return ResponseEntity.ok(this.tbCommentService.insert(tbComment));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("deleteById")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.tbCommentService.deleteById(id));
    }

}

